package es.uc3m.setichat.android.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import es.uc3m.setichat.android.R;
import es.uc3m.setichat.android.SeTIChatService;
import es.uc3m.setichat.android.SeTIChatXMPPController;

public class SeTIChatLoginActivity extends Activity{
	
	private boolean DEBUG = false;
	private SeTIChatService mBoundService;
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
	        // This is called when the connection with the service has been
	        // established, giving us the service object we can use to
	        // interact with the service.  Because we have bound to a explicit
	        // service that we know is running in our own process, we can
	        // cast its IBinder to a concrete class and directly access it.
	        mBoundService = ((SeTIChatService.LocalBinder)service).getService();
	        
	        DEBUG = mBoundService.getDebug();
	        
	        if(DEBUG)
	        	Log.d("LoginActivity", "bind service");
	    }

	    public void onServiceDisconnected(ComponentName className) {
	        // This is called when the connection with the service has been
	        // unexpectedly disconnected -- that is, its process crashed.
	        // Because it is running in our same process, we should never
	        // see this happen.
	    	
	    	if(DEBUG)
	        	Log.d("LoginActivity", "onServiceDisconnected: un-bounding the service");
	    	
	    }
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.init);
  
        Button login = (Button) findViewById(R.id.bt_login);
        
        if (mBoundService == null) {
			// Binding the activity to the service to get shared objects
			bindService(new Intent(SeTIChatLoginActivity.this,
					SeTIChatService.class), mConnection,
					Context.BIND_AUTO_CREATE);
		}
        
        
        login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
		        EditText aux = (EditText) findViewById(R.id.edit_email);
		        String email = aux.getText().toString();
		        
		        aux = (EditText) findViewById(R.id.edit_nick);
		        String nick = aux.getText().toString();
		        
		        aux = (EditText) findViewById(R.id.edit_psw);
		        String psw = aux.getText().toString();
		        
		        aux = (EditText) findViewById(R.id.edit_tlf);
		        String tlf = aux.getText().toString();
		        
		        if(DEBUG){
			        Log.d("login-activity",email);
			        Log.d("login-activity",nick);
			        Log.d("login-activity",tlf);
		        }
		        
		        mBoundService.email = email;
		        mBoundService.nick 	= nick;
		        mBoundService.mobil = tlf;
		        mBoundService.psw 	= psw;
		        
		        mBoundService.xmppController = new SeTIChatXMPPController(DEBUG,email,psw,mBoundService,true);
		        
		        
		        
			}
		});
        
        
        

	}
}
